package com.jumi.microservice.service.logistics.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.jumi.microservice.dto.logistics.excel.SelfWaitSend;
import com.jumi.microservice.service.logistics.admin.SendService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author by mazhao
 * @Classname SelfWaitSendListener
 * @Description TODO
 * @Date 2020/9/9 13:47
 */
public class SelfWaitSendListener extends AnalysisEventListener<SelfWaitSend> {

    private SendService sendService;

    private Long uid;

    private String name;

    List<SelfWaitSend> list = new ArrayList<>();

    private static final int BATCH_COUNT = 10000;
    @Override
    public void invoke(SelfWaitSend selfWaitSend, AnalysisContext analysisContext) {
        list.add(selfWaitSend);
        // 达到BATCH_COUNT了，需要去存储一次数据库，防止数据几万条数据在内存，容易OOM
//        if (list.size() >= BATCH_COUNT) {
//            saveData(list);
//            // 存储完成清理 list
//            list.clear();
//        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        //全部读完业务逻辑
        saveData(list);
    }

    public SelfWaitSendListener(SendService sendService,Long uid,String name) {
        this.sendService = sendService;
        this.name = name;
        this.uid  = uid;
    }

    public void saveData(List<SelfWaitSend> list){
        sendService.excelImportHandler(list,uid,name);
    }
}
